﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using DataObjects.ADO;
using Bussiness_Objects;

namespace WindowsFormsApplication1
{
    public partial class LapTheDG : DevComponents.DotNetBar.Controls.SlidePanel
    {

        #region Bien
        DocGia _docGia;
        SqlDocGia _sqlDocGia = new SqlDocGia();

        LoaiDocGia _loaiDG = new LoaiDocGia();
        SqlLoaiDocGia _sqlLoaiDG = new SqlLoaiDocGia();

        DataTable _tableLoaiDG;
        #endregion

        #region Khoi tao
        public LapTheDG()
        {
            InitializeComponent();

            this.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Left;            
            pic_error_NgSinh.Visible = false;            

            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
            this.SlideOutButtonVisible = false;
            UpdateTenLoaiDG();
            TinhHanThe();
        }

        public void ShowSlide()
        {            
            ResetField();
            this.IsOpen = true;
            UpdateTenLoaiDG();
            UpdateSize();
        }

        public void UpdateSize()
        {
            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
            grbThongTin.Location = new Point(
                this.Size.Width / 2 - grbThongTin.Size.Width / 2,
                this.Size.Height / 2 - grbThongTin.Size.Height / 2);
        }

        private void UpdateTenLoaiDG()
        {            
            _tableLoaiDG = _sqlLoaiDG.GetTable();
            
            cmbLoaiDG.DataSource = _tableLoaiDG;
            cmbLoaiDG.DisplayMember = _tableLoaiDG.Columns[1].ToString();
            cmbLoaiDG.ValueMember = _tableLoaiDG.Columns[0].ToString();         
        }

        private void ResetField()
        {
            txtTenDG.Text = null;
            cmbLoaiDG.Text = null;
            txtDiachi.Text = null;
            txtEmail.Text = null;
            dtime_NgayLT.Value = DateTime.Today;
            dtime_NgaySinh.Text = null;

            txtTenDG.Focus();
        }

        public void EditDataDocGia(DocGia docgia_edit)
        {
            txtTenDG.Text = docgia_edit.HoTen;
            cmbLoaiDG.SelectedValue = docgia_edit.MaDocGia;
            dtime_NgaySinh.Text = docgia_edit.NgaySinh;
            txtDiachi.Text = docgia_edit.DiaChi;
            txtEmail.Text = docgia_edit.Email;
            dtime_NgayLT.Text = docgia_edit.NgayLapThe;
            txtHanThe.Text = docgia_edit.NgatHetHan;
            
        }
        #endregion

        #region Click
        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;            
        } 
        
        private void lbl_error_TenLoaiDG_Click(object sender, EventArgs e)
        {            
            txtTenLoaiDG.Text = cmbLoaiDG.Text;
            txtTenLoaiDG.Focus();
        }

        private void butNhapLai_Click(object sender, EventArgs e)
        {
            ResetField();
        }

        private void butNhapDG_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTenDG.Text))
            {
                int maLDG = (int)cmbLoaiDG.SelectedValue;
                _docGia = new DocGia(0,
                                     maLDG,
                                     txtTenDG.Text,
                                     dtime_NgaySinh.Text,
                                     txtDiachi.Text,
                                     txtEmail.Text,
                                     dtime_NgayLT.Text,
                                     null,
                                     0);
                string result = _sqlDocGia.Insert(_docGia);
                if (result == "Success")
                {
                    panel_Result.Text = "Đã nhập độc giả thành công";
                    ResetField();
                }
                else
                    panel_Result.Text = result;
            }
            else
                panel_Result.Text = "Vui lòng nhập tên độc giả nếu muốn nhập";
        }

        private void butNhapLoaiDG_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTenLoaiDG.Text))
            {
                string text = txtTenLoaiDG.Text;
                _loaiDG.TenLoaiDocGia = txtTenLoaiDG.Text;
                string result = _sqlLoaiDG.Insert(_loaiDG);
                if (result == "Success")
                {
                    panel_Result.Text = "Đã nhập loại độc giả thành công";
                    cmbLoaiDG.Focus();
                    cmbLoaiDG.Text = txtTenLoaiDG.Text;
                    UpdateTenLoaiDG();
                    cmbLoaiDG.Focus();
                }
                else
                    panel_Result.Text = result;
            } 
            else
                panel_Result.Text = "Vui lòng nhập tên loại độc giả nếu muốn nhập";
        }

        private void dtime_NgaySinh_Leave(object sender, EventArgs e)
        {
            if (!MainForm._ThamSo.KiemTraTuoi(dtime_NgaySinh.Text))
            {
                pic_error_NgSinh.Visible = true;
                panel_Result.Text = MainForm._ThamSo.KiemTraTuoi_Error;
            }
            else
                pic_error_NgSinh.Visible = false;
        }
        #endregion        

        private void TinhHanThe()
        {
            txtHanThe.Text = MainForm._ThamSo.ThoiHanTHe(dtime_NgayLT.Text);
        }

        private void dtime_NgayLT_TextChanged(object sender, EventArgs e)
        {
            TinhHanThe();
        }

        private void txtTenDG_Leave(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.Controls.TextBoxX textbox = sender as DevComponents.DotNetBar.Controls.TextBoxX;
            if (!string.IsNullOrEmpty(textbox.Text))
            {                
                textbox.Text = Bussiness_Objects.BusinessRule.ValiadateText.UpcaseName(textbox.Text);
            }
        }

        private void txtEmail_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtEmail.Text))
            {
                if (!Bussiness_Objects.BusinessRule.ValidateEmail.CheckEmail(txtEmail.Text))
                {
                    pic_error_Email.Visible = true;
                    panel_Result.Text = "Email không hợp lệ";
                    txtEmail.Focus();
                }
                else
                    pic_error_Email.Visible = false;
            }
        }        
    }
}
